#!/usr/bin/env python3.9

COLOURSPACE="RGB"
#COLOURSPACE="YCbCr"
CARDSIZE=(320,256)
#CARDSIZE=(640,496)
(xmax, ymax) = CARDSIZE

background = (0,0,0)
foreground = 255

import math
from PIL import Image, ImageDraw, ImageFont
fnt = ImageFont.truetype("/usr/home/cjb/.fonts/PetMe64.ttf", 16)

im = Image.new(mode=COLOURSPACE, size=CARDSIZE, color=background)
d = ImageDraw.Draw(im)

def docircle (xc, yc, r, col):
    for y in range (ymax):
        dy = (yc - y) * (yc - y)
        for x in range (xmax):
            d = math.sqrt(dy + (xc - x) * (xc - x))
            if d < r:
                p = im.getpixel ((x,y))
                #i = 1.33 - (0.33 * (d // (r / 16)))
                n = 128
                i = (1/n) * (d // (r / n))
                d = abs(i*16 - int(i*16))
                if d < 0.5:
                    i = 0
                    if d < 0.25:
                        i = 1
                q = (int(p[0]+col[0]*i), int(p[1]+col[1]*i), int(p[2]+col[2]*i))
                if q[0] < 0:
                    q = (q[0]+256, q[1], q[2])
                if q[1] < 0:
                    q = (q[0], q[1]+256, q[2])
                if q[2] < 0:
                    q = (q[0], q[1], q[2]+256)
                if q[0] > 255:
                    q = (q[0]-256, q[1], q[2])
                if q[1] > 255:
                    q = (q[0], q[1]-256, q[2])
                if q[2] > 255:
                    q = (q[0], q[1], q[2]-256)

                im.putpixel ((x,y), q)


rx = xmax // 3

#docircle (xmax // 3, ymax // 3, rx, (127,0,0))
#docircle (2*xmax // 3, ymax // 3, rx, (64,127,0))
#docircle (xmax // 2, ymax - (ymax // 3), rx, (63,0,127))

docircle (xmax // 3, ymax // 3, rx, (255,0,0))
docircle (2*xmax // 3, ymax // 3, rx, (0,255,0))
docircle (xmax // 2, ymax - (ymax // 3), rx, (0,0,255))


#d.text ((xmax/2,4), "VK2CJB", font=fnt, fill=(255,128,128,255))
d.text ((8,ymax-20), "VK2CJB", font=fnt, fill=(255,255,255,255))
imrgb = im.convert("RGB")
s = "supercolourwheelrgb-"
s += str(xmax) + "x" + str(ymax) + ".png"
print (s)
imrgb.save(s)
